package cn.fboost.mtos.controller;

import cn.fboost.mtos.annotation.RoleIs;
import cn.fboost.mtos.dto.Response;
import cn.fboost.mtos.dto.internal.Session;
import cn.fboost.mtos.dto.request.AddressAddRequest;
import cn.fboost.mtos.dto.request.AddressUpdateRequest;
import cn.fboost.mtos.service.AddressService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 地址控制器
 */
@RestController
@RequestMapping("address")
@RoleIs({Session.Role.USER})
public class AddressController {
    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    /**
     * 获取用户配送地址列表
     *
     * @return {@link Response}
     */
    @GetMapping("list")
    public Response addressList() {
        return addressService.AddressList();
    }

    /**
     * 新增配送地址
     *
     * @param request {@link AddressAddRequest}
     * @return {@link Response}
     */
    @PostMapping("add")
    public Response createAddress(@RequestBody @Validated AddressAddRequest request) {
        return addressService.AddressAdd(request);
    }

    /**
     * 更新配送地址
     *
     * @param request {@link AddressUpdateRequest}
     * @return {@link Response}
     */
    @PostMapping("update")
    public Response updateAddress(@RequestBody @Validated AddressUpdateRequest request) {
        return addressService.AddressUpdate(request);
    }

    /**
     * 删除配送地址
     *
     * @param id 地址ID
     * @return {@link Response}
     */
    @PostMapping("delete/{id}")
    public Response deleteAddress(@PathVariable("id")
                                      @Validated 
                                      @NotNull(message = "地址ID不能为空")
                                      @Min(value = 1, message = "地址ID有误") Integer id) {
        return addressService.AddressDelete(id);
    }
}
